package cadc.actions;

import java.util.Iterator;
import java.util.List;

import cadc.daos.FileItemDao;
import cadc.daos.GroupDao;
import cadc.models.FileItem;
import cadc.models.Group;

@SuppressWarnings("serial")
public class Bianhao extends UserAction {
	private GroupDao groupDao;
	private FileItemDao fileItemDao;
	private String[] pres = {"A1","A2","A3","A4","B1","B2","B3","B4","B5","B6","C1","D1"};
	int[] nums = new int[12];
	public String execute(){
		List<Group> groups = groupDao.getBasicInfoListByStatus(3);	
		Iterator<Group> it = groups.iterator();
		while(it.hasNext()){
			Group g = it.next();
			String bianhao = getBianhao(g.getWorkType());
			groupDao.changeName(bianhao, (int) g.getId());
			FileItem fi = fileItemDao.getFileItemById((int) g.getWork().getId());
			String newName = bianhao+fi.getItemFileName().substring(fi.getItemFileName().lastIndexOf('.'));
			fileItemDao.changeName(newName, fi.getId());
		}
		return SUCCESS;
	}
	private String getBianhao(int workType){
		int num = ++nums[workType];
		if(num<10)return pres[workType]+"0"+num;
		else return pres[workType]+num;
	}
	public GroupDao getGroupDao() {
		return groupDao;
	}
	public void setGroupDao(GroupDao groupDao) {
		this.groupDao = groupDao;
	}
	public FileItemDao getFileItemDao() {
		return fileItemDao;
	}
	public void setFileItemDao(FileItemDao fileItemDao) {
		this.fileItemDao = fileItemDao;
	}
}
